const app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Component({
  properties: {
    receiveData: {
      type: JSON,
      value: 'default value',
    }
  },
  data: {
    someData: {},
    page:"1",
    shops:null,
    journey:[],//公里数
    listPage: {
      page: 1,
      pageSize: 20,
      totalSize: 0,
    },
  },
  ready:function(){
    this.getData();
    console.log("===receiveData===",this.data.receiveData);
    if(this.data.receiveData&&this.data.receiveData.jsonData.count){
        this.data.listPage.pageSize=this.data.receiveData.jsonData.count
    }
    if (app.globalData.setting.platformSetting.defaultColor && app.globalData.setting.platformSetting.defaultColor !=""){
      app.globalData.consoleFun("=====nearShop组件-defaultColor=====",[this.data.receiveData,app.globalData.setting.platformSetting.defaultColor])
      // 有默认色
      this.setData({
        defaultColor: app.globalData.setting.platformSetting.defaultColor
      })
    }
    else{
      // 没有默认色
      this.setData({
        defaultColor: app.globalData.setting.platformSetting.defaultColor
      })
    }
  },
  methods: {
      toProlinkUrl: function (e) {
        app.globalData.consoleFun("=====productList组件-tolinkUrl=====",[e.currentTarget.dataset.info])
        let productData = e.currentTarget.dataset.info;
        if(productData){
            productData=JSON.parse(productData)
        }
        let link="";
        if (productData.productType==6){
          link = "ticket_detail.html?productId=" + productData.id; 
        }else{
          link = "product_detail.html?productId=" + productData.id; 
        }
        app.globalData.linkEvent(link);
      },
      // 定位
      clickCatch: function (e) {
        console.log("===clickCatch===",e);
        let itemData=e.currentTarget.dataset.item;
        if(itemData&&itemData!=""){
            itemData=JSON.parse(itemData)
        }
        let latitude = itemData.latitude;
        let longitude = itemData.longitude;
        let name = itemData.shopName;
        let address = itemData.shopLocation;
        wx.openLocation({
          latitude: latitude,
          longitude: longitude,
          scale: 12,
          name: name,
          address: address
        });
      },
    tolinkUrl: function (e) {
      app.globalData.consoleFun("=====nearShop组件-link=====",[e.currentTarget.dataset.link])
      let linkUrl = e.currentTarget.dataset.link
      app.globalData.linkEvent(linkUrl)
    },
    getData:function(){
      let that = this;
      let shopName = app.globalData.setting.platformSetting.defaultShopBean.account.shopName
      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          let latitude = res.latitude
          let longitude = res.longitude
          app.globalData.consoleFun("=====nearShop组件-getLocation=====",[longitude,latitude,that.data.page])
          let pageParam = { 
            "longitude": longitude,
            "latitude": latitude,
            "pageObjectType":that.data.receiveData.pageObjectType||0,
            "pageObjectId":that.data.receiveData.pageObjectId||0
          }
          pageParam=Object.assign({},pageParam,that.data.listPage)
          if(that.data.receiveData.jsonData.shopTypeId){
              pageParam=Object.assign({},pageParam,{ "shopTypeId":that.data.receiveData.jsonData.shopTypeId})
          }
          app.globalData.consoleFun("=====nearShop组件-pageParam=====",[pageParam])
          let customIndex = app.globalData.AddClientUrl("/more_near_shops.html", pageParam, 'get', 1)
          app.globalData.showToastLoading('loading', true)
          wx.request({
            url: customIndex.url,
            header: app.globalData.header,
            method: 'GET',
            success: function (res) {
              if (res.data.errcode == 0) {
                that.data.listPage.pageSize = res.data.relateObj.pageSize
                that.data.listPage.totalSize = res.data.relateObj.totalSize
                let dataArr = that.data.shops
                if (that.data.listPage.page == 1) {
                  that.setData({ shops: null })
                }
                if ((!res.data.relateObj.result || res.data.relateObj.result.length == 0) || that.data.listPage.page==1) {
                  dataArr=[];
                } 
                dataArr = dataArr.concat(res.data.relateObj.result)
                let tagArray=[];
                for (let j = 0; j < dataArr.length; j++) {
                  // 获取公里数
                  dataArr[j].distance = app.globalData.getDistance(latitude, longitude, dataArr[j].latitude, dataArr[j].longitude)
                  if (dataArr[j].shopTag) {
                    tagArray = dataArr[j].shopTag.slice(1, -1).split("][")
                    dataArr[j].tagArray = tagArray;
                  }
                }
                console.log("=========dataArr=========", dataArr)
                that.setData({ shops: dataArr })
                console.log("===shops====", that.data.shops,)
                wx.hideLoading();
                that.setData({ loading: false })
              }else{
                wx.showToast({
                  title: '加载失败...',
                  icon: 'none',
                  duration: 2000,
                })
                wx.navigateBack(
                  { delta: 1, }
                )
              }
            },
            fail: function (res) {
              wx.hideLoading()
              app.globalData.loadFail()
            }
          })
        }
      })
    },
    onReachBottom: function () {
      let that = this
      console.log('组件===onReachBottom====',that.data.receiveData.jsonData.count)
      if ((that.data.listPage.totalSize > that.data.listPage.page * that.data.listPage.pageSize)&&that.data.receiveData.jsonData.count==-1) {
        that.data.listPage.page++
        that.getData();
      }
    },
  },
})